import { set as _set, merge as _merge } from 'lodash-es'

export default {
  setCurrentEditOne(state, data) {
    state.currentEditOne = data
  },
  changeStatusPreview(state, { type }) {
    state.currentEditStatus = type
  },
  updateSchemaUpdateTime(state) {
    state.schemaUpdateTime = Date.now()
  },
  initSchema(state, { metaData, codeData }) {
    // console.log('initSchemaStart', state.schema)
    state.schema.metaData = metaData
    state.schema.bannerConf = _merge({}, state.schema.bannerConf, codeData.bannerConf)
    state.schema.bottomConf = _merge({}, state.schema.bottomConf, codeData.bottomConf)
    state.schema.skinConf = _merge({}, state.schema.skinConf, codeData.skinConf)
    state.schema.baseConf = _merge({}, state.schema.baseConf, codeData.baseConf)
    state.schema.submitConf = _merge({}, state.schema.submitConf, codeData.submitConf)
    state.schema.questionDataList = codeData.questionDataList || []
    state.schema.logicConf = codeData.logicConf
    // console.log('initSchemaEnd', state.schema)
  },
  clearSchema(state) {
    state.schema = {
      metaData: null,
      bannerConf: {
        titleConfig: {
          mainTitle: '<h3 style="text-align: center">欢迎填写活动</h3>',
          subTitle: `<p>为了给您提供更好的服务，希望您能抽出几分钟时间，将您的感受和建议告诉我们，<span style="color: rgb(204, 0, 0)">期待您的参与！</span></p>`,
          applyTitle: ''
        },
        bannerConfig: {
          bgImage: '',
          bgImageAllowJump: false,
          bgImageJumpLink: '',
          videoLink: '',
          postImg: ''
        }
      },
      bottomConf: {
        logoImage: '',
        logoImageWidth: '28%'
      },
      skinConf: {
        backgroundConf: {
          color: '#fff'
        },
        themeConf: {
          color: '#0081fa'
        },
        contentConf: {
          opacity: 100
        }
      },
      baseConf: {
        begTime: '',
        endTime: '',
        language: 'chinese',
        showVoteProcess: 'allow',
        tLimit: 0,
        answerBegTime: '',
        answerEndTime: '',
        answerLimitTime: 0,
        anonymous: true,
        isUpdate: true,
        isDel: true,
        isTop: true,
        isMultiple: true,
        answerUser: [],
        adminUser: []
      },
      submitConf: {
        submitTitle: '',
        msgContent: {},
        confirmAgain: {
          is_again: false
        },
        link: ''
      },
      questionDataList: [],
      logicConf: {
        showLogicConf: []
      }
    }
  },
  setFormId(state, data) {
    state.formId = data
  },
  setDataFormAttributeList(state, data) {
    state.schema.metaData = _merge({}, state.schema.metaData, data)
  },

  addQuestion(state, { question, index }) {
    state.schema.questionDataList.splice(index, 0, question)
  },
  deleteQuestion(state, { index }) {
    state.schema.questionDataList.splice(index, 1)
  },
  moveQuestion(state, { index, range }) {
    let start, end
    if (range < 0) {
      // 向上移动
      start = index + range
      end = index
    } else if (range > 0) {
      // 向下移动
      start = index + 1
      end = index + range + 1
    } else {
      // 无变化
      return
    }
    const currentData = state.schema.questionDataList[index]
    // 新位置和老位置之间所有的问题
    const comparedList = state.schema.questionDataList.slice(start, end)
    if (range < 0) {
      // 向上移动
      state.schema.questionDataList.splice(index + range, 1 - range, currentData, ...comparedList)
    } else if (range > 0) {
      // 向下移动
      state.schema.questionDataList.splice(index, range + 1, ...comparedList, currentData)
    }
  },
  changeSchema(state, { key, value }) {
    _set(state.schema, key, value)
  },
  changeThemePreset(state, presets) {
    Object.keys(presets).forEach((key) => {
      _set(state.schema, key, presets[key])
    })
  },
  setQuestionDataList(state, data) {
    // console.log('setQuestionDataList', data)
    state.schema.questionDataList = data
  },
  updataQuestionList(state, data) {
    state.schema.metaData = _merge({}, state.schema.metaData, {dataFormAttributeList: data})
    data.forEach(item => {
      let attrContent = JSON.parse(item.attrContent)
      if (attrContent) {
        item.field = attrContent.field || ''
      }
    })
    state.schema.questionDataList.forEach((item) => {
      if (!item.attrId) {
        let tmpidx = data.findIndex(itm => itm.field === item.field)
        if (tmpidx > -1) {
          item.attrId = data[tmpidx].attrId
          item.formId = data[tmpidx].formId
        }
      }
    })
  },
}
